home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.04 Apr 96 / ThreadingAppleEvents.sit / ThreadingAppleEvents / Grant's CGI Code 1.0b13 / Util / CGI.h / CGI.h
Encoding:
C/C++ Source or Header  |  1996-01-26  |  8.1 KB  |  309 lines  |  [TEXT/CWIE]

  1. #pragma once
  2. /*****
  3.  *
  4.  *    Grant's CGI Framework (Common Grant Interface :-)
  5.  *        http://arpp1.carleton.ca/grant/mac/grantscgi/
  6.  *
  7.  *    CGI.h
  8.  *
  9.  *    standard types and function prototypes for cgi applications
  10.  *    See the Read Me or CGI.c for instructions on using the CGI Utilities
  11.  *
  12.  *    #include this file in your source files that need to access the cgi module
  13.  *
  14.  *    This is a support file for "Grant's CGI Framework".
  15.  *    Please see the license agreement that accompanies the distribution package
  16.  *    for licensing details.
  17.  *
  18.  *    Copyright ©1995 by Grant Neufeld
  19.  *
  20.  *    http://arpp1.carleton.ca/grant/
  21.  *    gneufeld@ccs.carleton.ca
  22.  *    grant@acm.org
  23.  *
  24.  *****/
  25.  
  26. #include "MyConfiguration.h"
  27. #if kCompileWithCGICode
  28.  
  29. #include <Threads.h>
  30.  
  31. /***  CONSTANT DECLARATIONS  ***/
  32.  
  33. #define kCGIParamMaxSize        32769
  34.  
  35. #define kCGIHTTPMethodGet                "GET"
  36. #define kCGIHTTPMethodPost                "POST"
  37. #define kCGIHTTPMethodGetConditional    "GET_CONDITIONAL"
  38.  
  39. #define kCGIFormFieldDelimiter    '='
  40. #define kCGIFormFieldSeparator    '&'
  41.  
  42. /* Apple events */
  43.  
  44. #define kAEClassCGI            'WWWΩ'
  45. #define kAEIDSearchDoc        'sdoc'
  46.  
  47. /* CGI event parameters */
  48. #define kCGIpath_args            '----'
  49. #define kCGIhttp_search_args    'kfor'
  50. #define kCGIusername            'user'
  51. #define kCGIpassword            'pass'
  52. #define kCGIfrom_user            'frmu'
  53. #define kCGIclient_address        'addr'
  54. #define kCGIpost_args            'post'
  55. #define kCGImethod                'meth'
  56. #define kCGIserver_name            'svnm'
  57. #define kCGIserver_port            'svpt'
  58. #define kCGIscript_name            'scnm'
  59. #define kCGIcontent_type        'ctyp'
  60. #define kCGIreferer                'refr'
  61. #define kCGIuser_agent            'Agnt'
  62. #define kCGIaction                'Kact'
  63. #define kCGIaction_path            'Kapt'
  64. #define kCGIclient_ip            'Kcip'
  65. #define kCGIfull_request        'Kfrq'
  66. #define kCGIversion                'Pvrs'
  67. #define kCGIconnection            'Kcid'
  68.  
  69. /* for an official listing of the maximum sizes for CGI parameters,
  70.     <http://www.biap.com/datapig/mrwheat/cgi_params.html> */
  71. #define kCGIMaxpath_args        1024
  72. #define kCGIMaxhttp_search_args    1024
  73. #define kCGIMaxusername            32
  74. #define kCGIMaxpassword            32
  75. #define kCGIMaxfrom_user        128
  76. #define kCGIMaxclient_address    256
  77. #define kCGIMaxpost_args        32768
  78. #define kCGIMaxmethod            32
  79. #define kCGIMaxserver_name        256        /* server address? */
  80. #define kCGIMaxserver_port        16
  81. #define kCGIMaxscript_name        1024
  82. #define kCGIMaxcontent_type        64
  83. #define kCGIMaxreferer            1024
  84. #define kCGIMaxuser_agent        256
  85. #define kCGIMaxaction            32
  86. #define kCGIMaxaction_path        1024
  87. #define kCGIMaxclient_ip        32
  88. #define kCGIMaxfull_request        4096
  89. #define kCGIMaxversion            32768    /* ••• missing the correct value */
  90. #define kCGIMaxconnection        4
  91.  
  92. /* Action Names */
  93. #define kCGIActionNameCGI        "CGI"
  94. #define kCGIActionNameACGI        "ACGI"
  95.  
  96. /* Send Partial event */
  97. #define kMyAESendPartial        'SPar'
  98.  
  99. #define kCGIPartialData            '----'
  100. #define kConnectionIDKeyword    'Kcid'
  101. #define kMoreKeyword            'Kmor'
  102.  
  103. #define kCGIPartialStartString    "<SEND_PARTIAL>"
  104.  
  105.  
  106. /***  TYPE DECLARATIONS  ***/
  107.  
  108. #if kCompileWithCGIFormHandling
  109. typedef struct
  110. {
  111.     char *    name;
  112.     char *    value;
  113. } CGIFormField;
  114. #endif
  115.  
  116. #if kCompileWithCGImethod
  117. typedef enum
  118. {
  119.     HTTP_UNDEFINED,
  120.     HTTP_get,
  121.     HTTP_post,
  122.     HTTP_getConditional
  123. } HTTPMethod;
  124. #endif
  125.  
  126. typedef struct
  127. {
  128.     /** the following fields should be treated as public read-only **/
  129.     
  130.     #if kCompileWithCGIpath_args
  131.     char *        path_args;            /* '----' path_args            */
  132.     #endif
  133.     #if kCompileWithCGIhttp_search_args
  134.     char *        http_search_args;    /* 'kfor' http_search_args    */
  135.     #endif
  136.     #if kCompileWithCGIusername
  137.     char    username[kCGIMaxusername];    /* 'user' username            */
  138.     #endif
  139.     #if kCompileWithCGIpassword
  140.     char    password[kCGIMaxpassword];    /* 'pass' password            */
  141.     #endif
  142.     #if kCompileWithCGIfrom_user
  143.     char *        from_user;            /* 'frmu' from_user            */
  144.     #endif
  145.     #if kCompileWithCGIclient_address
  146.     char *        client_address;        /* 'addr' client_address    */
  147.     #endif
  148.     #if kCompileWithCGIpost_args
  149.     char *        post_args;            /* 'post' post_args            */
  150.     #endif
  151.     #if kCompileWithCGImethod
  152.     HTTPMethod    method;                /* 'meth' method            */
  153.     #endif
  154.     #if kCompileWithCGIserver_name
  155.     char *        server_name;        /* 'svnm' server_name        */
  156.     #endif
  157.     #if kCompileWithCGIserver_port
  158.     short        server_port;        /* 'svpt' server_port        */
  159.     #endif
  160.     #if kCompileWithCGIscript_name
  161.     char *        script_name;        /* 'scnm' script_name        */
  162.     #endif
  163.     #if kCompileWithCGIcontent_type
  164.     char *        content_type;        /* 'ctyp' content_type        */
  165.     #endif
  166.     #if kCompileWithCGIreferer
  167.     char *        referer;            /* 'refr' referer            */
  168.     #endif
  169.     #if kCompileWithCGIuser_agent
  170.     char *        user_agent;            /* 'Agnt' user_agent        */
  171.     #endif
  172.     
  173.     #if kCompileWithCGIActionSupport
  174.     char    action[kCGIMaxaction];    /* 'Kact' action            */
  175.     char *        action_path;        /* 'Kapt' action_path        */
  176.     #endif
  177.     
  178.     #if kCompileWithCGIclient_ip
  179.     char    client_ip[kCGIMaxclient_ip];    /* 'Kcip' client_ip            */
  180.     #endif
  181.     #if kCompileWithCGIfull_request
  182.     char *        full_request;        /* 'Kfrq' full_request        */
  183.     #endif
  184.     #if kCompileWithCGIversion
  185.     char *        version;            /* 'Pvrs' version            */
  186.     #endif
  187.     
  188.     #if kCompileWithCGISendPartial
  189.     long        connection;            /* 'Kcid' connection        */
  190.     #endif
  191.     
  192.     #if kCompileWithCGIFormHandling
  193.     CGIFormField *    formFields;        /* the fields from form submission */
  194.     long            totalFields;    /* total number of fields    */
  195.     #endif
  196.     
  197.     /** private fields that probably should not be touched outside
  198.         of the CGI.c file **/
  199.     AppleEvent        appleEvent;        /* originating appleEvent    */
  200.     AppleEvent        replyEvent;        /* apple event reply record    */
  201.     
  202.     #if kCompileWithThreadedAppleEvents
  203.     Boolean            suspended;        /* whether the AE has been suspended */
  204.     #endif
  205.     
  206. //    #if kCompileWithThreadedAppleEvents
  207. //    ThreadID        thread;            /* ID of the CGI handle's thread */
  208. //    #endif
  209.     
  210.     /** public fields to be filled in **/
  211.     #if kCompileWithCGIResponseDataAsHandle
  212.     Handle        responseData;        /* data to return to the client        */
  213.     #else
  214.     char *        responseData;        /* data to return to the client        */
  215.     #endif
  216.     long        responseSize;        /* size in bytes of the response    */
  217.     
  218.     #if kCompileWithCGIRefCon
  219.     long        refCon;                /* field for storing custom data    */
  220.     #endif
  221. } CGIrecord;
  222.  
  223. typedef CGIrecord ** CGIHdl;
  224.  
  225.  
  226. /***  GLOBAL DECLARATIONS  ***/
  227.  
  228. #ifdef __CGISegment__
  229. #define _GLOBAL_    
  230. #else
  231. #define _GLOBAL_    extern
  232. #endif
  233.  
  234. /* these are globals for holding the standard http headers.
  235.     One of the headers must be prepended to the data returned in the Apple Event */
  236.  
  237. _GLOBAL_    Str255    gHTTPHeaderOK;            /* use data returned after header    */
  238. _GLOBAL_    Str255    gHTTPHeaderRedirect;    /* redirect client to different url    */
  239. _GLOBAL_    Str255    gHTTPHeaderErr;            /* an application level error        */
  240. #if kCompileWithCGISendPartial
  241. _GLOBAL_    Str255    gHTTPHeaderPush;        /* multipart server push            */
  242. #endif
  243.  
  244. _GLOBAL_    long    gHTTPHeaderOKSize;
  245. _GLOBAL_    long    gHTTPHeaderRedirectSize;
  246. _GLOBAL_    long    gHTTPHeaderErrSize;
  247. #if kCompileWithCGISendPartial
  248. _GLOBAL_    long    gHTTPHeaderPushSize;
  249. #endif
  250.  
  251. #undef _GLOBAL_
  252.  
  253.  
  254. /***  FUNCTION PROTOTYPES  ***/
  255.  
  256.         OSErr        InitCGIUtil                ( void );
  257.         
  258.         #if kCompileWithCGIFormHandling
  259.         
  260.         CGIFormField *    CGIFormFieldsFromArgs    ( char *, long *, short * );
  261.         CGIFormField *    CGIFormFieldsFindRecord    ( CGIHdl, char * );
  262.         char *            CGIFormFieldsFindValue    ( CGIHdl, char * );
  263.         void            CGIFormFieldsDispose    ( CGIFormField * );
  264.         
  265.         #endif    /* kCompileWithCGIFormHandling */
  266.         
  267.         #if kCompileWithCGIActionSupport
  268.         Boolean        CGIActionIsCGIorACGI    ( CGIHdl );
  269.         #endif
  270.         
  271.         void        CGIDecodeSpecialChars    ( char * );
  272.         char *        CGIEncodeSpecialChars    ( char * );
  273.         void        CGICharToHex            ( unsigned char, char * );
  274.         
  275.         void        CGIPathToMacPath        ( char * );
  276.         
  277. pascal    OSErr        CGIAESearchDoc        ( AppleEvent *, AppleEvent *, long );
  278.         
  279.         #if kCompileWithCGISendPartial
  280.         OSErr        CGIAESendPartial    ( CGIHdl, char *, long, Boolean );
  281.         #endif    /* kCompileWithCGISendPartial */
  282.  
  283.  
  284. /***  EXTERNAL FUNCTION PROTOTYPES  ***/
  285.  
  286.     /* this is the function which you must define for your particular application */
  287.     void        MyCGIProcess    ( CGIHdl );
  288.     
  289.     /* this function is called once at startup time.
  290.         Put any initialization you need to do in it. */
  291.     Boolean        MyCGIStartup    ( void );
  292.  
  293.     /* this function is called at quitting time.
  294.         Put any cleanup you need to do in it. */
  295.     Boolean        MyCGIQuit        ( Boolean );
  296.  
  297.  
  298. #else    /* if not kCompileWithCGICode */
  299.  
  300.     /* these are defined like this here so the StartupApplication function
  301.         doesn't have to be messed with when compiling without the CGI module */
  302.     #define InitCGIUtil()    (noErr)
  303.     #define MyCGIStartup()    (true)
  304.     #define MyCGIQuit()        (true)
  305.  
  306. #endif    /* kCompileWithCGICode */
  307.  
  308. /***  EOF  ***/
  309.